package mn.idax.exchange.otc.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mn.idax.exchange.common.bean.ResponseData;
import mn.idax.exchange.otc.annotation.RequestLimit;
import mn.idax.exchange.otc.constant.RequestUrlConstant;
import mn.idax.exchange.otc.enums.RequestLimitType;
import mn.idax.exchange.otc.exception.IdaxException;
import mn.idax.exchange.otc.service.AliyunOSSService;
import mn.idax.exchange.otc.util.AliyunOSSUtils;
import mn.idax.exchange.otc.util.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: dison
 * @Date: 2018/8/8 09:54
 * @Description: 阿里云附件上传
 */
@Api(value = "阿里云附件上传",tags = {"附件上传"})
@RestController
@RequestMapping(RequestUrlConstant.MODULE_FILE)
public class AliyunOSSController {

    @Autowired
    private AliyunOSSService aliyunOSSService;

    //评价订单
    @RequestMapping(value = RequestUrlConstant.FILE_UPLOAD,headers = {"content-type=multipart/form-data"} ,method = RequestMethod.POST)
    @ApiOperation(value = "附件上传",notes = "<b>注意点:</b><br/> 1.上传文件类型 <br/>2.上传文件大小【沈迪雄/自测通过】")
    public ResponseData upload(@RequestParam("file") @ApiParam(name = "file",required = true) MultipartFile uploadFile,
                               HttpServletResponse response) throws IdaxException, IOException {
        long start = System.currentTimeMillis();

        MultipartFile[] files = {uploadFile};
        AliyunOSSUtils.verifyMultipartFiles(files);
        List<String> listAliyunPath = new ArrayList<>();
        for (MultipartFile file : files) {
            String aliyunPath = this.aliyunOSSService.uploadStream(file.getInputStream(),file.getOriginalFilename());
            listAliyunPath.add(aliyunPath);
        }

        long end = System.currentTimeMillis();
        long cost = end - start;
        LogUtils.info(this.getClass(),"AliyunUpload upload cost " + cost);
        return ResponseData.success(listAliyunPath);
    }

    /**
     *
     * 功能描述:
     *      聊天文件上传接口
     * @param:
     * @return:
     * @auther: dison
     * @date: 2018/8/14 12:27
     */
    @RequestMapping(value = RequestUrlConstant.FILE_UPLOAD_CHAT,headers = {"content-type=multipart/form-data"} ,method = RequestMethod.POST)
    @ApiOperation(value = "聊天附件上传",notes = "<b>注意点:</b><br/> 1.上传文件类型 <br/>2.上传文件大小【沈迪雄/自测通过】")
//    @RequestLimit(period = 100,count = 100,limitType = RequestLimitType.IP)
    public ResponseData uploadChat(@RequestParam("file") @ApiParam(name = "file",required = true) MultipartFile uploadFile) throws IdaxException, IOException {
        MultipartFile[] files = {uploadFile};
        AliyunOSSUtils.verifyMultipartFiles(files);
        String imageKey = null;
        for (MultipartFile file : files) {
            imageKey = this.aliyunOSSService.uploadChat(file.getInputStream(),file.getOriginalFilename());
        }
        return ResponseData.success(imageKey);
    }

}
